package cl.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cl.model.dto.Region;
import cl.model.dto.Vehicle;

public class DataService {
	private DataService(){
	}
	
	private static class SingletonHolder { 
		private static final DataService INSTANCE = new DataService();
	}
	 
	public static DataService getInstance() {
		return SingletonHolder.INSTANCE;
	}
	/**
	 * 
	 * @param nombreProductor
	 * @return
	 */
	public Vehicle crearVehicle(String marca, 
								String modelo,
								String version,
								String anio,
								String tipoVehicle,
								String carroceria,
								String color,
								String cilindrada,
								String direccion,
								String aireAcondicionado,
								String alzavidrios,
								String espejos,
								String cierre,
								String catalitico,
								String combustible,
								String puertas,
								String portal,
								String techo,
								String patente,
								String precio,
								String comentario,
								String vendedor,
								String telefono,
								String ciudad,
								String correo,
								String direccionFisica,
								String potencia,
								String unicoDueno,
								String llantas,
								String contacto,
								String comuna,
								String radio,
								String codigoVehiculo,
								Region region) {
	    
		Transaction tx = null;
	    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Vehicle vehicle = null;
		try{
	        tx=session.beginTransaction();       
	        vehicle= new Vehicle();           
	        vehicle.setMarca(marca);
			vehicle.setModelo(modelo);
			vehicle.setVersion(version);
			vehicle.setAnio(anio);
			vehicle.setTipoVehicle(tipoVehicle);
			vehicle.setCarroceria(carroceria);
			vehicle.setColor(color);
			vehicle.setCilindrada(cilindrada);
			vehicle.setDireccion(direccion);
			vehicle.setAireAcondicionado(aireAcondicionado);
			vehicle.setAlzavidrios(alzavidrios);
			vehicle.setCierre(cierre);
			vehicle.setCatalitico(catalitico);
			vehicle.setCombustible(combustible);
			vehicle.setPuertas(puertas);
			vehicle.setPortal(portal);
			vehicle.setTecho(techo);
			vehicle.setPatente(patente);
			vehicle.setPrecio(precio);
			vehicle.setComentario(comentario);
			vehicle.setVendedor(vendedor);
			vehicle.setTelefono(telefono);
			vehicle.setCiudad(ciudad);
			vehicle.setCorreo(correo);
			vehicle.setDireccionFisica(direccionFisica);
			vehicle.setPotencia(potencia);
			vehicle.setUnicoDueno(unicoDueno);
			vehicle.setLlantas(llantas);
			vehicle.setContacto(contacto);
			vehicle.setComuna(comuna);
			vehicle.setRadio(radio);
			vehicle.setCodigoVehiculo(codigoVehiculo);
	        vehicle.setRegion(region);  
	        
	        session.save(vehicle);
	        tx.commit();
       }catch (HibernateException e) {
            e.printStackTrace();            
            if (tx != null) try {
                tx.rollback();
        } catch (HibernateException e1) {
            e1.printStackTrace();
        }
        } finally {
            try {
                if (session.isOpen()) session.close();
            } catch (HibernateException e1) {            
                e1.printStackTrace();
            }
        }
    	return vehicle;
	}
	
	/*
	 * 
	 */
	public Region getRegion(String nombreRegion){
	  Region region = null;
	  Session session = null;
	  Transaction tx = null;
	  session = HibernateUtil.getSessionFactory().getCurrentSession();
	    try{
    		tx=session.beginTransaction(); 
    		region = (Region) session.createQuery(
            "from Region as r where r.codigoRegion = ?")
            .setString(0, nombreRegion.trim()).uniqueResult();
    		if(region!=null){
    			region.getVehicles().size();
    			tx.commit();
    		}
       }catch (HibernateException e) {
            e.printStackTrace();           
       }finally {
            try {
                if (session.isOpen()) session.close();
            } catch (HibernateException e1) {            
                e1.printStackTrace();
            }
        }      	
       if (region == null){
    	   region = crearRegion(nombreRegion);
       }
       	return region;
	}
	
	/*
	 * 
	 */
	private Region crearRegion(String nombreRegion) {
		  	Transaction tx = null;
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			Region region = null;
			try{
		        tx=session.beginTransaction();       
		        region=new Region();           
		        region.setCodigoRegion(nombreRegion);
		                 
		        session.save(region);
		        tx.commit();
	       }catch (HibernateException e) {
	            e.printStackTrace();            
	            if (tx != null) try {
	                tx.rollback();
	        } catch (HibernateException e1) {
	            e1.printStackTrace();
	        }
	        } finally {
	            try {
	                if (session.isOpen()) session.close();
	            } catch (HibernateException e1) {            
	                e1.printStackTrace();
	            }
	        }
	    	return region;
		}
	public void actualizarVehicle(Vehicle vehicle){
		Transaction tx = null;
	    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try{
	        tx=session.beginTransaction();  
	        session.saveOrUpdate(vehicle);
	        tx.commit();
       }catch (HibernateException e) {
            e.printStackTrace();            
            if (tx != null) try {
                tx.rollback();
        } catch (HibernateException e1) {
            e1.printStackTrace();
        }
        } finally {
            try {
                if (session.isOpen()) session.close();
            } catch (HibernateException e1) {            
                e1.printStackTrace();
            }
        }
	}
	
	
	
	
}